package com.fh.controller.system.weixin;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fh.controller.system.weixin.resp.SNSUserInfo;
import com.fh.controller.system.weixin.resp.WeixinOauth2Token;
import com.fh.controller.system.weixin.wxutil.OAuthUtil;

public class OAuthServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public OAuthServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		// 用户同意后，能获得code
		String code = request.getParameter("code");
		String state = request.getParameter("state");
		String openId = null;
		SNSUserInfo snsUserInfo = new SNSUserInfo();
		// 用户同意授权
		if (!"authdeny".equals(code)) {
			// 获取网页授权access_token
			WeixinOauth2Token weixinOauth2Token = OAuthUtil
					.getOauth2AccessToken("wxb7f6c9100ba0a6cc",
							"e8dcbbde25f54500058a6687324e3e3b", code);
			// 网页接口凭证
			String access_token = weixinOauth2Token.getAccessToken();
			openId = weixinOauth2Token.getOpenId();
			// 获取用户信息
			snsUserInfo = OAuthUtil.getSNSUserInfo(access_token, openId);

			// 设置要传递的参数
		} else {
			// 获取网页授权access_token
			WeixinOauth2Token weixinOauth2Token = OAuthUtil
					.getOauth2AccessToken("wx04bc1b6710b04f12",
							"c95a38d0769886c8f3be980be697c8db", code);
			// //网页接口凭证
			String access_token = weixinOauth2Token.getAccessToken();
			openId = weixinOauth2Token.getOpenId();
			snsUserInfo.setOpenId(openId);
			request.setAttribute("openId", openId);
			System.out.println("openId=" + openId);
		}
		System.out.println("用户的openId========================"
				+ snsUserInfo.getOpenId());
		// 跳转到制定页面
		/**
		 * 更具state 的值 判断要跳转的页面
		 */
		if ("all".equals(state)) {
			// response.sendRedirect("");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
